package com.aj.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.OpinionRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.utils.Contants;

public class OpinionActivity extends BaseActivity {
	private EditText opinion_edittext;
	private Button sure_button;
	private String userid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_opinion);
		initView();
	}

	private void initView() {
		opinion_edittext = (EditText) findViewById(R.id.opinion_edittext);
		sure_button = (Button) findViewById(R.id.sure_button);
		ButtonClick buttonClick = new ButtonClick();
		sure_button.setOnClickListener(buttonClick);
		if (getIntent().hasExtra(Contants.USERINFOUSERID_STRING)) {
			userid = getIntent().getStringExtra(Contants.USERINFOUSERID_STRING);
		}
	}

	class ButtonClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.sure_button:
				if (opinion_edittext.getText().toString().trim().length() < 6) {
					Toast.makeText(OpinionActivity.this,
							getString(R.string.commit_opinion_error_text),
							Toast.LENGTH_LONG).show();
					break;
				}
				showProgressDialog();
				OpinionRequest request = new OpinionRequest();
				request.excute(OpinionActivity.this, userid, opinion_edittext
						.getText().toString().trim(), "1", new IHttpCallBack() {
					@Override
					public void success(String result) {
						dimissProgressDialog();
						try {
							Common common = new Common(new JSONObject(result));
							if (common != null
									&& common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
								Toast.makeText(
										OpinionActivity.this,
										getString(R.string.commit_opinion_success_text),
										Toast.LENGTH_LONG).show();
								finish();
							} else {
								Toast.makeText(OpinionActivity.this,
										common.getErrorMsg(), Toast.LENGTH_LONG)
										.show();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void fail(String result) {
						dimissProgressDialog();
						try {
							Common common = new Common(new JSONObject(result));
							Toast.makeText(OpinionActivity.this,
									common.getErrorMsg(), Toast.LENGTH_LONG)
									.show();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
				break;
			}
		}
	}
}
